package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInSNDetailsAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationGetSNsByPackageBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationGetSNsByPackageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.List;

/**
 * 物料明细(序列号)
 *
 * @author 詹建宇
 * @time on 2018/4/4 10:37
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInSNDetailsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private String mat_id;
    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;//物料
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList;//仓位列表
    private AllocationInSNDetailsAdapter adapter;

    private String condition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_listview);

        initView(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        getExtra(this);
        initData(this);
    }

    private void getExtra(Activity activity) {
        mat_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.MAT_ID);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);

        listView.setPullUpFlag(false);
        listView.setPullDownFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("物料明细");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String result = check();
                if (TextUtils.isEmpty(result)) {
                    goDie();
                } else {
                    DialogUtils.getConfirmDialog(activity, result, new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            dialog.dismiss();
                            goDie();
                        }
                    }).show();
                }
            }
        });
        condition = "";
        itemListBean = AllocationUtils.getItemListBeanByMatId(activity, mat_id);
        stockPositionList = itemListBean.getStock_position_list();
        stockPositionList = AllocationUtils.addNoCodePosition(stockPositionList);
        addListData(activity, stockPositionList, listView);

        setmScanListener((String scanResult, int length) -> {
            String condition = null;
            switch (QRCodeUtils.getQRCodeType(scanResult)) {
                case QRCodeConstant.CY_TYPE_POSITION:
                case QRCodeConstant.CY_TYPE_PACKAGE:
                case QRCodeConstant.CY_TYPE_SN:
                    condition = scanResult;
                    break;
                default:
                    break;
            }

            if (!TextUtils.isEmpty(condition)) {
                getType(this, condition);
                AllocationInSNDetailsActivity.this.condition = condition;
            }

        }, QRCodeConstant.CY_TYPE_POSITION, QRCodeConstant.CY_TYPE_PACKAGE, QRCodeConstant.CY_TYPE_SN);
    }

    private void addListData(Activity activity, List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list, ListView listView) {
        adapter = new AllocationInSNDetailsAdapter(activity, condition, itemListBean, list, new ClickListener() {
            @Override
            public void onNoDoubleClickListener(String condition) {
                AllocationInSNDetailsActivity.this.condition = condition;
                getType(activity, condition);
            }
        });
        listView.setAdapter(adapter);
    }

    private void getType(Activity activity, String condition) {
        AllocationGetSNsByPackageRequest.request(activity, condition, itemListBean.getLocation_id(), new GsonBeanListener<AllocationGetSNsByPackageBean>() {
            @Override
            public void onSuccess(AllocationGetSNsByPackageBean bean) {
                switch (bean.getBody().getType()) {
                    case "1":
                    case "2":
                        AllocationUtils.setSNsInDefaultPosition(activity, mat_id, bean);
                        initData(activity);
                        break;
                    case "3":
                        AllocationUtils.setDefaultSNsInPosition(activity, mat_id, bean);
                        initData(activity);
                        break;
                }
            }
        });
    }

    public interface ClickListener {
        void onNoDoubleClickListener(String condition);
    }

    @Override
    public void onBackPressed() {
        String result = check();
        if (TextUtils.isEmpty(result)) {
            goDie();
        } else {
            DialogUtils.getConfirmDialog(this, result, new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    dialog.dismiss();
                    goDie();
                }
            }).show();
        }
    }

    private String check() {
        String msg = null;
        int inNum = adapter.getInNum();
        int outNum = adapter.getOutNum();
        if (inNum != outNum) {
            msg = "出库数量与入库数量不相等,确认退出?";
        }
        return msg;
    }

    private void goDie() {
        AllocationUtils.saveIsOkThisMatOfSN(this, mat_id, adapter.getInNum(), adapter.getOutNum());
        finish();
    }
}
